package com.yanqu.road.logic.activity.foxcard.game.effect;

import com.yanqu.road.entity.activity.foxcard.FoxHandCard;
import com.yanqu.road.entity.activity.foxcard.FoxSaleCard;
import com.yanqu.road.entity.activity.foxcard.config.FoxCardEffectCardConfig;
import com.yanqu.road.entity.activity.foxcard.config.FoxCardMagicWeaponConfig;
import com.yanqu.road.entity.activity.foxcard.config.FoxCardSorceryConfig;
import com.yanqu.road.entity.activity.foxcard.config.FoxCardSpellConfig;
import com.yanqu.road.logic.activity.foxcard.FoxCardConfig;
import com.yanqu.road.logic.activity.foxcard.game.*;
import com.yanqu.road.utils.RandomHelper;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
 * 每x回合，战斗开始时，产生x张某类型的牌
 * 回合|数量|cardType|品质|增强效果|版本效果|印章效果
 */
public class FoxCardEffect13 extends FoxCardEffectBase {
    @Override
    protected void effectHandle(FoxCardGameLogic gameLogic, FoxHandCard handCard) {
        if (triggerType != eFoxCardTriggerType.ROUND_START) {
            return;
        }

        int perRound = Integer.valueOf(effectConfig.getParamsList().get(0));
        if (perRound == 0) {
            return;
        }

        int round = gameLogic.getGameData().getRound();
        if (round % perRound != 0) {
            return;
        }

        FoxCardConfig config = gameLogic.getConfig();

        int cardNum = Integer.valueOf(effectConfig.getParamsList().get(1));
        int cardType = Integer.valueOf(effectConfig.getParamsList().get(2));
        int cardQuality = Integer.valueOf(effectConfig.getParamsList().get(2));
        if (cardType == eFoxCardType.EFFECT.getValue()) {
            if (cardQuality == 0) { //小丑牌随机品质
                cardQuality = gameLogic.getConfig().randomShopEffectCardQuality(null);
            }
            int versionEffect = Integer.valueOf(effectConfig.getParamsList().get(5));
            List<FoxSaleCard> foxSaleCards = gameLogic.getConfig().randomEffectCard(gameLogic.getGameData().getEffectCards(), cardQuality, cardNum, versionEffect, gameLogic.getCouponAddValue(eFoxCardCouponType.COUPON8.getValue()));
            for (FoxSaleCard saleCard : foxSaleCards) {
                versionEffect = saleCard.getVersionEffect();
                if (!config.isBuffType8(versionEffect) && gameLogic.getGameData().getEffectCards().size() >= gameLogic.getGameData().getEffectCardNumLimit()) {
                    continue;
                }
                FoxCardShowInfo showInfo = gameLogic.addNewEffectCard(saleCard.getConfigId(), versionEffect);
                showInfo.setTriggerCards(Collections.singletonList(effectCard));
            }
        } else if (cardType == eFoxCardType.HAND.getValue()) {
            for (int i = 0; i < cardNum; i++) {
                int point = config.getRandomHelper().next(1, config.getPointToScoreMap().size() + 1);
                int color = config.getRandomHelper().next(1, FoxCardGameLogic.CARD_COLOR_NUM + 1);
                int reinforceEffect = Integer.valueOf(effectConfig.getParamsList().get(4));
                if (reinforceEffect == -1) {
                    reinforceEffect = config.randomReinforceEffect(cardType);
                }
                int versionEffect = Integer.valueOf(effectConfig.getParamsList().get(5));
                if (versionEffect == -1) {
                    versionEffect = config.randomVersionEffect(cardType, gameLogic.getCouponAddValue(eFoxCardCouponType.COUPON8.getValue()));
                }
                int sealEffect = Integer.valueOf(effectConfig.getParamsList().get(6));
                if (sealEffect == -1) {
                    sealEffect = config.randomHandCardSealEffect();
                }

                FoxCardShowInfo showInfo = gameLogic.addNewHandCard(point, color, reinforceEffect, versionEffect, sealEffect);
                showInfo.setTriggerCards(Collections.singletonList(effectCard));
            }
        } else if (cardType == eFoxCardType.MAGIC_WEAPON.getValue()) {
            List<FoxCardMagicWeaponConfig> magicWeaponConfigs = config.randomMagicWeapons(cardNum, new HashSet<>());
            for (FoxCardMagicWeaponConfig magicWeaponConfig : magicWeaponConfigs) {
                int versionEffect = Integer.valueOf(effectConfig.getParamsList().get(5));
                if (!config.isBuffType8(versionEffect) && gameLogic.getGameData().getConsumeCards().size() >= gameLogic.getGameData().getConsumeCardNumLimit()) {
                    break;
                }
                FoxCardShowInfo showInfo = gameLogic.addNewConsumeCard(cardType, magicWeaponConfig.getId(), versionEffect);
                showInfo.setTriggerCards(Collections.singletonList(effectCard));
            }
        } else if (cardType == eFoxCardType.SPELL.getValue()) {
            List<FoxCardSpellConfig> spellConfigs = config.randomSpells(cardNum);
            for (FoxCardSpellConfig spellConfig : spellConfigs) {
                int versionEffect = Integer.valueOf(effectConfig.getParamsList().get(5));
                if (!config.isBuffType8(versionEffect) && gameLogic.getGameData().getConsumeCards().size() >= gameLogic.getGameData().getConsumeCardNumLimit()) {
                    break;
                }
                FoxCardShowInfo showInfo = gameLogic.addNewConsumeCard(cardType, spellConfig.getId(), versionEffect);
                showInfo.setTriggerCards(Collections.singletonList(effectCard));
            }
        } else if (cardType == eFoxCardType.SORCERY.getValue()) {
            List<FoxCardSorceryConfig> foxCardSorceryConfigs = config.randomSorceryList(cardNum);
            for (FoxCardSorceryConfig foxCardSorceryConfig : foxCardSorceryConfigs) {
                int versionEffect = Integer.valueOf(effectConfig.getParamsList().get(5));
                if (!config.isBuffType8(versionEffect) && gameLogic.getGameData().getConsumeCards().size() >= gameLogic.getGameData().getConsumeCardNumLimit()) {
                    break;
                }
                FoxCardShowInfo showInfo = gameLogic.addNewConsumeCard(cardType, foxCardSorceryConfig.getId(), versionEffect);
                showInfo.setTriggerCards(Collections.singletonList(effectCard));
            }
        }
    }
}
